home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / snz128s / src / addgroup.c < prev    next >
C/C++ Source or Header  |  1994-05-23  |  8KB  |  285 lines

  1. /*
  2.     SNEWS 2.0
  3.  
  4.     addgroup - add a new newsgroup
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License, version 1, as
  13.     published by the Free Software Foundation.
  14.  
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     See the file COPYING, which contains a copy of the GNU General
  21.     Public License.
  22.  
  23.     Atari version ported by Graham Judd - gjudd@siward.demon.co.uk
  24.  */
  25.  
  26. /*---------------------------- Source Control ------------------------------*/
  27.  
  28. /*
  29.  * $Id: ADDGROUP.C,v 1.2 1994/02/05 18:46:56 gbj Exp user $
  30.  */
  31.  
  32. /****************************************************************************
  33. *   19 Jul 92   1.2     GT  Extra parameter in find_news_group ().          *
  34. *    5 Sep 92   1.3    MSM  Snews 1.90                                      *
  35. *                           Information message                             *
  36. *                           Add to ng file                                  *
  37. *  21 Nov 92    1.4    MSM  Wrong line ending corrected                     *
  38. *  20 Mar 93    1.5    MSM  Wrong line ending in ng file also corrected     *
  39. *   3 Jun 93    1.6    MSM  Snews 2.0                                       *
  40. *                           make ng an optional file                        *
  41. ****************************************************************************/
  42.  
  43. #include <io.h>
  44. #include <fcntl.h>
  45.  
  46. #include "defs.h"
  47. #include "locking.h"
  48.  
  49. #ifdef ATARI
  50. void delay(size_t millisecs);
  51. #include "fileops.h"
  52. #endif
  53.  
  54. INFO            my_stuff;
  55.  
  56.  
  57. /*---------------------------- main ---------------------------------------*/
  58. void            main(int argc, char *argv[])
  59. {
  60.  
  61.     /*
  62.      * Add newsgroups.  All that has to be done is stick the new group
  63.      * in the active file, after checking that it doesn't already exist.
  64.      * It aint pretty, but it works.
  65.      */
  66.  
  67.     int             i;
  68.     char            buf[256], buf2[256], *p, txt_name[20];
  69.     int             added, added2;
  70.     time_t          t;
  71.     ACTIVE         *gp;
  72.     FILE           *tmp;
  73.     FILE           *active_file, *new_active_file;
  74.     int             junk_flag;              /* nz - posting to junk */
  75.  
  76. #ifndef __TURBOC__
  77. #ifndef ATARI
  78.     clock_t         goal;
  79. #endif
  80. #endif
  81.  
  82.     if (argc > 1) {
  83.  
  84.         if (!load_stuff()) {
  85.             fprintf(stderr, "Couldn't read rc info\n");
  86.         }
  87.  
  88.         /*
  89.          * Check for active file.  If there is none:
  90.          * - create it with a single entry 'junk'.
  91.          * - ensure that the newsbase directory exists
  92.          * - create an empty junk text file and index file
  93.          */
  94.  
  95.         printf("Demon Internet Simple news Version %d.%02d [build %d]\n", rmj, rmm, rup);
  96.         printf("Add New Newsgroup.\n");
  97.         sprintf(buf, "%sactive", my_stuff.news_dir);
  98.         if (access(buf, 0) != 0) {
  99.             active_file = fopen(buf, "wb");
  100.             fprintf(active_file, "junk 00000000 00000000 00000000 y\n");
  101.             fclose(active_file);
  102.         }
  103.  
  104.         /* ensure that the news base file exists - just try to create it */
  105.  
  106.         sprintf(buf, "%snewsbase", my_stuff.news_dir);
  107.         mkdir(buf);
  108.         sprintf(buf, "%snewsbase\\00000000", my_stuff.news_dir);
  109.         if (access(buf, 0) != 0) {
  110.             if ((tmp = fopen(buf, "wb")) == NULL) {
  111.                 fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  112.                 exit(1);
  113.             }
  114.             fclose(tmp);
  115.         }
  116.         sprintf(buf, "%snewsbase\\00000000.idx", my_stuff.news_dir);
  117.         if (access(buf, 0) != 0) {
  118.             if ((tmp = fopen(buf, "wb")) == NULL) {
  119.                 fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  120.                 exit(1);
  121.             }
  122.             fclose(tmp);
  123.         }
  124.  
  125.         load_active_file();
  126.         close_active();
  127.         
  128.         for (i = 1; i < argc; i++) {
  129.  
  130.             /* check that the group doesn't already exist */
  131.             gp = find_news_group(argv[i], &junk_flag);
  132.  
  133.             if ((stricmp(argv[i], "junk") != 0) &&
  134.                 (stricmp(gp->group, "junk") == 0)) {
  135.  
  136.                 /* open the active file */
  137.                 sprintf(buf, "%sactive", my_stuff.news_dir);
  138.                 if ((active_file = fopen(buf, "rb")) == NULL) {
  139.                     fprintf(stderr, "addgroup: cannot open active file\n");
  140.                     exit(1);
  141.                 }
  142.                 /* create new one */
  143.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  144.                 if ((new_active_file = fopen(buf, "wb")) == NULL) {
  145.                     fprintf(stderr, "addgroup: cannot create %s\n", buf);
  146.                     exit(1);
  147.                 }
  148.                 added = added2 = FALSE;
  149.                 if (active_file != NULL) {
  150.  
  151.                     /* delay ensures names are different - ahem */
  152. #ifdef __TURBOC__
  153.                     delay(1000);
  154. #else
  155. #ifdef ATARI
  156.                     delay(1000);
  157. #else
  158.                     goal = 2000 + clock();
  159.                     while (goal > clock());
  160. #endif
  161. #endif
  162.                     sprintf(txt_name, "%ld", time(&t));
  163.                     if (strlen(txt_name) > 8) {
  164.                         strcpy(txt_name, txt_name + (strlen(txt_name) - 8));
  165.                     }
  166.  
  167.                     /*
  168.                      * Check newbase name is not already used.
  169.                      * If not create an empty newsbase and index files.
  170.                      */
  171.  
  172.                     sprintf(buf, "%snewsbase\\%s", my_stuff.news_dir, txt_name);
  173.                     if (access(buf, 0) != 0) {
  174.  
  175.                         if ((tmp = fopen(buf, "wb")) == NULL) {
  176.                             fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  177.                             exit(1);
  178.                         }
  179.                         fclose(tmp);
  180.                         sprintf(buf, "%snewsbase\\%s.idx", my_stuff.news_dir, txt_name);
  181.                         if ((tmp = fopen(buf, "wb")) == NULL) {
  182.                             fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  183.                             exit(1);
  184.                         }
  185.                         fclose(tmp);
  186.  
  187.                     }
  188.                     else {
  189.                         fprintf(stderr, "addgroup: newsbase name collision - try again\n");
  190.                         fclose(new_active_file);
  191.                         unlink(buf);
  192.                         exit(1);
  193.                     }
  194.  
  195.                     while (fgets(buf, 255, active_file) != NULL) {
  196.  
  197.                         strcpy(buf2, buf);
  198.                         p = strtok(buf2, " \t");
  199.  
  200.                         /* the active file entry */
  201.                         if ((stricmp(argv[i], p) < 0) && !added) {
  202.                             fprintf(new_active_file, "%s %s 00000000 00000000 y\r\n",
  203.                                     argv[i], txt_name);
  204.                             added = TRUE;
  205.                             fprintf(stderr, "addgroup: News group %s successfully added\n", argv[i]);
  206.  
  207.                         }
  208.                         if (fputs(buf, new_active_file) == EOF) {
  209.                             fprintf(stderr, "addgroup: couldn't write new active file\n");
  210.                         }
  211.                     }
  212.                 }
  213.                 if (!added) {
  214.                     fprintf(new_active_file, "%s %s 00000000 00000000 y\r\n",
  215.                             argv[i], txt_name);
  216.                     fprintf(stderr, "addgroup: News group %s successfully added\n", argv[i]);
  217.                 }
  218.                 fclose(active_file);
  219.                 fclose(new_active_file);
  220.  
  221.                 sprintf(buf, "%sactive.bak", my_stuff.news_dir);
  222.                 unlink(buf);
  223.                 sprintf(buf2, "%sactive", my_stuff.news_dir);
  224.                 rename(buf2, buf);
  225.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  226.                 rename(buf, buf2);
  227.  
  228.                 sprintf(buf, "%sng", my_stuff.news_dir);
  229.                 if ((active_file = fopen(buf, "rb")) == NULL) {
  230. /*                    fprintf(stderr, "addgroup: permission file ng does not exist\n"); */
  231.                 }
  232.                 else {
  233.                     sprintf(buf, "%sng.new", my_stuff.news_dir);
  234.                     if ((new_active_file = fopen(buf, "wb")) == NULL) {
  235.                         fprintf(stderr, "addgroup: cannot create %s\n", buf);
  236.                         exit(1);
  237.                     }
  238.                     while (fgets(buf, 255, active_file) != NULL) {
  239.  
  240.                         strcpy(buf2, buf);
  241.                         p = strtok(buf2, " \t");
  242.  
  243.                         /* the active file entry */
  244.                         if ((stricmp(argv[i], p) < 0) && !added2) {
  245.                             fprintf(new_active_file, "%s\r\n", argv[i]);
  246.                             added2 = TRUE;
  247.                             fprintf(stderr, "addgroup: Posting permitted to %s\n", argv[i]);
  248.                         }
  249.                         if (fputs(buf, new_active_file) == EOF) {
  250.                             fprintf(stderr, "addgroup: couldn't write new ng file\n");
  251.                         }
  252.                     }
  253.  
  254.                     if (!added2) {
  255.                         fprintf(new_active_file, "%s\r\n", argv[i]);
  256.                         fprintf(stderr, "addgroup: Posting permitted to %s\n", argv[i]);
  257.                     }
  258.                     (void) fclose(active_file);
  259.                     (void) fclose(new_active_file);
  260.  
  261.                     sprintf(buf, "%sng.bak", my_stuff.news_dir);
  262.                     unlink(buf);
  263.                     sprintf(buf2, "%sng", my_stuff.news_dir);
  264.                     rename(buf2, buf);
  265.                     sprintf(buf, "%sng.new", my_stuff.news_dir);
  266.                     rename(buf, buf2);
  267.                 }
  268.  
  269.             }
  270.             else {
  271.                 fprintf(stderr, "addgroup: newsgroup %s already exists\n", argv[1]);
  272.             }
  273.  
  274.         }
  275.  
  276.         close_active_file();
  277.  
  278.     }
  279.     else {
  280.         printf("USAGE: addgroup  newsgroup names....\n");
  281.     }
  282.  
  283. }
  284.  
  285.